using System;
using Gtk;

public partial class MainWindow : Gtk.Window
{
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{
		Build ();
	}

	string[] resumo = new string[10];
	protected void QuitApp (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
	
	protected virtual void ShowInformation (object sender, System.EventArgs e)
	{
		string[] d = {"Elísio G. Ribeiro", "Engenharia Informática", "Linguagem Programação", "2ºano"};
		MessageDialog md = new MessageDialog(this,0,MessageType.Info,ButtonsType.Close,false,
		                                     "Nomee: {0} \nCurso: {1} \nDisciplina {2} \nAlno: {3}",d);
		
		if(md.Run() == (int)ResponseType.Close){
			md.Destroy();
		}	
	}
	
	protected virtual void SaveToSelectedFile (object sender, System.EventArgs e)
	{
		FileChooserDialog dialogOpen = new FileChooserDialog("Escolher um Ficheiro", this,
		                                                     FileChooserAction.Save, "Cancelar", 
		                                                     ResponseType.Cancel, "Guardar", ResponseType.Accept);
				
		int rsp = dialogOpen.Run();
		if(rsp == (int)ResponseType.Accept){
			
			//this.buttonConverter.Sensitive = true;	
			//this.convertAction1.Sensitive = true;
			this.entryGuardarBd.Text = dialogOpen.Filename.ToString();
			dialogOpen.Destroy();
		}
		else{
			dialogOpen.Destroy();
		}
	}
	
	protected virtual void QuitApp (object sender, System.EventArgs e)
	{
		Application.Quit ();
	}
	
	protected virtual void CopySelectedText (object sender, System.EventArgs e)
	{
	}
	
	protected virtual void ConvertGivenFile (object sender, System.EventArgs e)
	{
		resumo.SetValue(this.entryFichCsv.Text, 1);
		resumo.SetValue(resumo[1].Split('/')[resumo[1].Split('/').Length-1], 0);
		resumo.SetValue(this.entryGuardarBd.Text, 3);
		resumo.SetValue(resumo[3].Split('/')[resumo[3].Split('/').Length-1], 2);
		
		Console.WriteLine("Ficheiro Escolhido:\n{0} \n{1}\n" +
								"\nFicheiro Salvo:\n{2}\n{3}\n",resumo);
		/*for(int k = 0; k < resumo.Length; k++){
			//this.textviewInfo.Buffer;
			Console.WriteLine("Ficheiro Escolhido:\n{0} \n{1}\n" +
								"\nFicheiro Salvo:\n{2}\n{3}\n",resumo);
		}*/
	}
	
	protected virtual void ExportToADocument (object sender, System.EventArgs e)
	{
	}
	
	protected virtual void ShowCalculateOptions (object sender, System.EventArgs e)
	{
		this.GtkLabelTitulo.LabelProp = Mono.Unix.Catalog.GetString("<b>Cálculo de Estatisticas</b>");
        
		//this.GtkLabelTitulo.Text = "<b>Cálculo de Estatisticas<b>";
		this.convertAction1.Sensitive = true;
		this.statisticOptions.Sensitive = false;
	}
	
	protected virtual void ShowConvertOptions (object sender, System.EventArgs e)
	{
		this.GtkLabelTitulo.LabelProp = Mono.Unix.Catalog.GetString("<b>Conversão / Criação</b>");
        
		//this.GtkLabelTitulo.Text = "<b>Conversão / Criação</b>";
		this.convertAction1.Sensitive = false;
		this.statisticOptions.Sensitive = true;
	}
	
	protected virtual void OnClearActionActivated (object sender, System.EventArgs e)
	{
		this.entryFichCsv.Text = "";
		this.entryGuardarBd.Text = "";
		this.textviewInfo.Buffer.Clear();
		this.buttonConverter.Sensitive = false;
		this.convertAction1.Sensitive = false;
		this.clearAction.Sensitive = false;
		this.labelStatus.Text = "";
	}
	
	protected virtual void GetToOpenPath (object sender, System.EventArgs e)
	{
		 FileChooserDialog dialogOpen = new FileChooserDialog("Escolher um Ficheiro", this,
		                                                     FileChooserAction.Open, "Cancelar", 
		                                                     ResponseType.Cancel, "Abrir", ResponseType.Accept);
		
		FileFilter fFilter = new FileFilter();
        fFilter.Name = "Ficheiros Csv (*.csv)";
		fFilter.AddPattern("*.csv");
        dialogOpen.AddFilter(fFilter);
		
		int rsp = dialogOpen.Run();
		if(rsp == (int)ResponseType.Accept){
			this.entryFichCsv.Text = dialogOpen.Filename.ToString();
			dialogOpen.Destroy();
		}
		else{
			dialogOpen.Destroy();
		}
	}
	
	protected virtual void OnEntryFichCsvTextInserted (object o, Gtk.TextInsertedArgs args)
	{
		this.clearAction.Sensitive = true;
		//verify if the selected files exists and have right extention
		if(this.entryFichCsv.Text != ""){
			if((!this.entryFichCsv.Text.EndsWith(".csv")) || (!System.IO.File.Exists(this.entryFichCsv.Text.ToString()))){
				this.entryFichCsv.ModifyBase(StateType.Normal, new Gdk.Color(255,0,0));
			}			
		}
		else{
				this.entryFichCsv.ModifyBase(StateType.Normal, new Gdk.Color(255,255,255));
			}
		this.labelStatus.Text = "Endereço do Ficheiro Csv a Converter";
	}
	
	protected virtual void OnEntryGuardarBdChanged (object sender, System.EventArgs e)
	{
		this.clearAction.Sensitive = true;
		if(this.entryFichCsv.Text.ToString() != ""){			
			this.buttonConverter.Sensitive = true;	
			this.convertAction1.Sensitive = true;			
		}
		this.labelStatus.Text = "Endereço do Ficheiro sqlite Guardado";
	}
	
	protected virtual void OnEntryFichCsvChanged (object sender, System.EventArgs e)
	{this.clearAction.Sensitive = true;
		//verify if the selected files exists and have right extention
		if(this.entryFichCsv.Text != ""){
			if((!this.entryFichCsv.Text.EndsWith(".csv")) || (!System.IO.File.Exists(this.entryFichCsv.Text.ToString()))){
				this.entryFichCsv.ModifyBase(StateType.Normal, new Gdk.Color(255,0,0));
			}			
		}
		else{
				this.entryFichCsv.ModifyBase(StateType.Normal, new Gdk.Color(255,255,255));
			}
		this.labelStatus.Text = "Endereço do Ficheiro Csv a Converter";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
